import {
  selectCourseNoteById,
  saveCourseNote
} from '@/api/noter'
export default {
  props: {
    options: {
      type: Object,
      default: function() {
        return {
          disabled: false
        }
      }
    }
  },
  data() {
    return {
      datas: null,
      params: {
        courseCatalogId: this.$route.query.courseCatalogId
      },
      form: {
        courseCatalogId: this.$route.query.courseCatalogId,
        courseNoteContent: '笔记内容'
      }
    }
  },
  created() {},
  watch: {
    $route: {
      handler() {
        this.params.courseCatalogId = this.$route.query.courseCatalogId
        this.form.courseCatalogId = this.$route.query.courseCatalogId
        this.getDatas()
        // 深度监听，同时也可监听到param参数变化
      },
      deep: true
    }
  },
  methods: {
    getDatas() {
      const _this = this
      selectCourseNoteById(_this.params).then(response => {
        if (response.code === 20000 || response.code === 200) {
          // _this.$message({
          //   type: 'success',
          //   message: '操作成功'
          // })
          _this.form = {
            courseCatalogId: this.$route.query.courseCatalogId || '',
            courseNoteContent: response.data.courseNoteContent || ''
          }
        } else {
          // _this.$message({
          //   type: 'error',
          //   message: '操作失败'
          // })
        }
      })
    },
    saveCourseNote() {
      if (this.form.courseCatalogId) {
        saveCourseNote(this.form).then(response => {
          if (response.code === 200 || response.code === 20000) {
            this.$message({
              type: 'success',
              message: '操作成功'
            })
          } else {
            this.$message({
              type: 'error',
              message: '操作失败'
            })
          }
        })
      } else {
        this.$message({
          type: 'error',
          message: '请先选择左侧章节课时'
        })
      }
    }
  }
}
